package mapEditor
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.filesystem.*;
	import flash.geom.Point;
	import flash.net.FileFilter;
	import flash.utils.describeType;
	
	import iso.IsoPoint;
	
	import mapEditor.common.DialogBoxManager;
	import mapEditor.common.MapAssetManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.model.EditorModel;
	import mapEditor.view.AssetPanelView;
	import mapEditor.view.CreateMapView;
	import mapEditor.view.MapAssetBase;
	import mapEditor.view.MapSceneView;
	import mapEditor.view.TopMenuView;
	import mapEditor.view.panels.PanelBaseView;
	import mapEditor.view.panels.PanelListView;
	import mapEditor.view.panels.PanelPutView;
	import mapEditor.view.panels.PanelToolsView;
	import mapEditor.vo.PanelType;
	import mapEditor.vo.ToolType;
	
	[SWF(width = "1000",height = "600",frameRate = "25")]
	public class MapEditor extends Sprite
	{
		private var _model:EditorModel;
		private var _topMenuView:TopMenuView;
		private var _panelListView:PanelListView;
		private var _assetPanelView:AssetPanelView;
		private var _mapSceneView:MapSceneView;
		private var _panelBaseView:PanelBaseView;
		private var _panelPutView:PanelPutView;		
		private var _panelToolsView:PanelToolsView;
		private var _stage:Stage;
		private var _createMapView:CreateMapView;
		
		public function MapEditor(stage:Stage)
		{
			_stage=stage;
			_model=new EditorModel();
			
			DialogBoxManager.Instance.register(_stage);
			
			setView();
			setEvent();
			//			testOpenMap();
		}
		
		private function setView():void
		{
			_topMenuView=new TopMenuView(_stage, _model);
			_panelToolsView=new PanelToolsView(_model);
			_panelToolsView.y=_topMenuView.height;
			_panelListView=new PanelListView(_model);
			_panelListView.y=_topMenuView.height;
			_panelListView.x=_stage.stageWidth-_panelListView.width;
			_assetPanelView=new AssetPanelView(_model);
			_assetPanelView.x=_stage.stageWidth-_panelListView.width-_assetPanelView.width;
			_assetPanelView.y=_topMenuView.height;
			
			_mapSceneView=new MapSceneView(_model);
			_mapSceneView.y=_topMenuView.height;
			_mapSceneView.x=_assetPanelView.width;
			addChild(_mapSceneView);
			
			addChild(_topMenuView);
			addChild(_panelListView);
			addChild(_panelToolsView);
		}
		
		private function setEvent():void
		{
			_model.addEventListener(MapEditorEvent.TOOL_TYPE_CHANGE, __onToolTypeChange);
			_model.addEventListener(MapEditorEvent.PANEL_TYPE_CHANGE, __onPanelTypeChange);		
			_mapSceneView.addEventListener(MapEditorEvent.MAP_SELECT_ASSET, __mapSelectAsset);
			_mapSceneView.addEventListener(MapEditorEvent.MAP_SELECT_ASSET_POS_CHANGE, __mapSelectAssetPosChange);	
			_assetPanelView.addEventListener(Event.REMOVED_FROM_STAGE, __onAssetPanelViewRemovedFromStage);
			_assetPanelView.addEventListener(Event.ADDED_TO_STAGE, __onAssetPanelViewAddedToStage);
			_topMenuView.addEventListener(MapEditorEvent.MAP_NEW_CREATE, __onMapNewCreateComplete);
		}
		
		private function __onAssetPanelViewRemovedFromStage(evt:Event):void
		{
			_panelListView.panelLib.gotoAndStop(1);
		}
		
		private function __onAssetPanelViewAddedToStage(evt:Event):void
		{
			_panelListView.panelLib.gotoAndStop(2);
		}
		
		private function __onPnlPutAssetRemovedFromStage(evt:Event):void
		{
			_panelListView.panelAttrib.gotoAndStop(1);
		}
		
		private function __onPnlPutAssetAddedToStage(evt:Event):void
		{
			_panelListView.panelAttrib.gotoAndStop(2);
		}
		
		/**
		 *当操作工具类型改变时 
		 */		
		private function __onToolTypeChange(evt:MapEditorEvent):void
		{
			switch(evt.date.toString())
			{
				case ToolType.MAP_ADD_ASSET://增加物件
					actionMapAddAsset();
					break;
				case ToolType.OPEN_MAP://打开地图
					openMap();
					break;
				case ToolType.SAVE_MAP://保存地图
					mapFileSave();
					break;
				case ToolType.MAP_ATTRIB://地图属性
					openMapAttribView();
					break;
			}
		}
		
		private function openMapAttribView():void
		{			
			if(_createMapView)
			{
				if(_createMapView.parent) _createMapView.parent.removeChild(_createMapView);
				_createMapView.dispose();
			}
			_createMapView=null;
			
			_createMapView=new CreateMapView(_model);
			_createMapView.addEventListener(MapEditorEvent.MAP_NEW_CREATE, __onMapAttribChange);
			DialogBoxManager.Instance.show(_createMapView, true, false);
		}
		
		private function __onMapAttribChange(evt:MapEditorEvent):void
		{
//			_mapSceneView.updateMapGrid();
//			_mapSceneView.updateMapRoad(_model.mapVO.roadArray);
		}
		
		/**
		 *当操作面板类型改变时 
		 */		
		private function __onPanelTypeChange(evt:MapEditorEvent):void
		{
			switch(evt.date.toString())
			{
				case PanelType.PANEL_ATTRIB:
					if(_panelBaseView && _panelBaseView.parent)
					{
						_panelBaseView.parent.removeChild(_panelBaseView);
					}
					else
					{
						if(_panelBaseView)
						{
							addChild(_panelBaseView);
						}
						else
						{
							setAssetPanelAttrib();
						}
					}
					break;
				case PanelType.PANEL_LIB:
					if(_assetPanelView.parent)
					{
						_assetPanelView.parent.removeChild(_assetPanelView);
					}
					else
					{
						addChild(_assetPanelView);
					}
					break;
			}
			
			layoutPanel();
		}
		
		/**
		 *布局控制面板 
		 */		
		private function layoutPanel():void
		{
			if(_panelBaseView && _panelBaseView.parent)
			{
				_panelBaseView.x=_stage.stageWidth-_panelBaseView.width-_panelListView.width;
			}
			
			if(_assetPanelView && _assetPanelView.parent)
			{
				if(_panelBaseView) _panelBaseView.x-=_assetPanelView.width;
			}
		}
		
		/**
		 *当激活地图物件增加时操作 
		 */		
		private function actionMapAddAsset():void
		{
			setAssetPanelAttrib(_model.assetName, _model.assetLink, _model.assetIsGrid, _model.assetIsFloor, _model.assetIsBottom, _model.assetIsMirror);
		}
		
		private function onPnlPutClose(evt:Event):void
		{
			if(_panelBaseView && _panelBaseView.parent) _panelBaseView.parent.removeChild(_panelBaseView);
		}
		
		private function __mapSelectAsset(evt:MapEditorEvent):void
		{
			var mab:MapAssetBase=evt.date as MapAssetBase;
			
			setAssetPanelAttrib(mab.mapAssetVO.assetName, mab.mapAssetVO.link, mab.mapAssetVO.isGrid, mab.mapAssetVO.isFloor, mab.mapAssetVO.isBottom, mab.mapAssetVO.isMirror, mab);
		}
		
		private function __mapSelectAssetPosChange(evt:MapEditorEvent):void
		{
			var pt:IsoPoint=evt.date as IsoPoint;
			updatePanelPutAssetViewData(null, pt);
		}		
		
		private function setAssetPanelAttrib(name:String="", link:String="", isGrid:Boolean=false, isFloor:Boolean=false, isBottom:Boolean=false, isMirror:Boolean=false, mapAssetBase:MapAssetBase=null):void
		{
			if(!_panelBaseView)
			{
				_panelBaseView=new PanelBaseView("物件属性");
				_panelBaseView.addEventListener(Event.CLOSE, onPnlPutClose)
				_panelBaseView.addEventListener(Event.REMOVED_FROM_STAGE, __onPnlPutAssetRemovedFromStage);
				_panelBaseView.addEventListener(Event.ADDED_TO_STAGE, __onPnlPutAssetAddedToStage);
				_panelBaseView.y=_topMenuView.height;
				_panelBaseView.x=_stage.stageWidth-_panelBaseView.width-_panelListView.width-_assetPanelView.width;
				
				_panelPutView=new PanelPutView(_model);
				_panelBaseView.addItem(_panelPutView);
			}
			addChild(_panelBaseView);
			
			if(!link || link=="" || link.length<=0) return;
			var _itemPreview:*=MapAssetManager.getAssetByType(link);
			var classType:String=String(describeType(_itemPreview).@base);
			switch(classType)
			{
				case "flash.display::BitmapData":
					_panelPutView.previewObject=new Bitmap(_itemPreview as BitmapData);
					break;
				case "flash.display::MovieClip":
					_panelPutView.previewObject=_itemPreview as MovieClip;
					break;
			}
			_itemPreview=null;
			_panelPutView.panelPutAssetView.txtDescription.text=name+"\n"+link;
			_panelPutView.panelPutAssetView.txtObjectType.text=classType.split('::')[1];
			_panelPutView.panelPutAssetView.chkIsGrid.selected=isGrid;
			_panelPutView.panelPutAssetView.chkIsFloor.selected=isFloor;
			_panelPutView.panelPutAssetView.chkIsBottom.selected=isBottom;
			_panelPutView.panelPutAssetView.chkIsMirror.selected=isMirror;
			
			_model.assetIsBottom=isBottom;
			_model.assetIsFloor=isFloor;
			_model.assetIsMirror=isMirror;
			_model.assetIsGrid=isGrid;
			
			if(mapAssetBase) updatePanelPutAssetViewData(mapAssetBase, mapAssetBase.position);
			
			if(_panelBaseView.parent) _panelListView.panelAttrib.gotoAndStop(2); else _panelListView.panelAttrib.gotoAndStop(1);
			if(_assetPanelView.parent) _panelListView.panelLib.gotoAndStop(2); else _panelListView.panelLib.gotoAndStop(1);
		}
		
		private function updatePanelPutAssetViewData(mab:MapAssetBase=null, pt:IsoPoint=null):void
		{
			if(pt)
			{
				_panelPutView.panelPutAssetView.txtX.text=pt.x.toString();
				_panelPutView.panelPutAssetView.txtY.text=pt.y.toString();
				_panelPutView.panelPutAssetView.txtZ.text=pt.z.toString();
			}
			
			if(mab)
			{
				_panelPutView.panelPutAssetView.txtWitdh.text=mab.width.toString();
				_panelPutView.panelPutAssetView.txtHeight.text=mab.height.toString();
			}
		}
		
		/**
		 * 当执行创建新地图操作完成时
		 */		
		private function __onMapNewCreateComplete(evt:MapEditorEvent):void
		{
			_model.isNewMap=true;
			
			//清除原物件数据
			for(var m:int=0;m<_model.mapVO.assetArray.length;m++)
			{
				if(_model.mapVO.assetArray[m])
				{
					if(_model.mapVO.assetArray[m].parent) _model.mapVO.assetArray[m].parent.removeChild(_model.mapVO.assetArray[m]);
					_model.mapVO.assetArray[m].dispose();
				}
				_model.mapVO.assetArray[m]=null;
			}
			_model.mapVO.assetArray=new Vector.<MapAssetBase>();
			
			_mapSceneView.updateMapGrid();
			_mapSceneView.updateMapAsset();
			_mapSceneView.updateMapRoad(_model.mapVO.roadArray);
		}
		
		/**
		 *保存当前的地图数据 
		 */		
		private function mapFileSave():void
		{
			if(_model.isNewMap)
			{//如果是新建的地图，则选择保存路径相关操作
				var file:File = File.applicationDirectory;
				file.browseForSave("地图文件保存至");
				file.addEventListener(Event.SELECT,__onFileSelectHandle);
			}
			else
			{
				if(_model.mapFilePath) saveFile(_model.mapFilePath);
			}
		}
		
		/**
		 *当在保存时选定完成本地跟径 
		 */		
		private function __onFileSelectHandle(e:Event=null):void
		{
			saveFile(e.target.nativePath);
		}
		
		/**
		 *执行保存地图文件 
		 */		
		private function saveFile(path:String):void
		{
			var file:File = new File(path);
			var fileStream:FileStream=new FileStream();
			fileStream.open(file,FileMode.WRITE);
			var content:String="";
			content+="<root>";
			content+="	<name>"+_model.mapVO.mapName+"</name>";
			content+="	<mapType>"+_model.mapVO.mapType+"</mapType>";			
			content+="	<description>"+_model.mapVO.mapDescription+"</description>";
			content+="	<row>"+_model.mapVO.mapRow+"</row>";
			content+="	<cols>"+_model.mapVO.mapCols+"</cols>";
			content+="	<size>"+_model.mapVO.cellSize+"</size>";
			content+="	<map>";	
			content+="		<road>"+_model.mapVO.roadArray+"</road>";
			var arrPath:String="";
			for(var i:int=0;i<_model.mapVO.assetLibPath.length;i++)
			{
				arrPath+=_model.mapVO.assetLibPath[i].path;
				if(i<_model.mapVO.assetLibPath.length-1) arrPath+=",";
			}
			content+="		<asset assetLibPath=\""+arrPath+"\">";
			for each(var mab:MapAssetBase in _model.mapVO.assetArray)
			{
				content+="			<item link=\""+mab.mapAssetVO.link+"\" x=\""+mab.x+"\" y=\""+mab.y+"\" z=\""+mab.z+"\"" + (mab.point ? " baseX=\""+mab.point.x+"\" baseY=\""+mab.point.y+"\"" : "") + " width=\""+mab.width+"\" height=\""+mab.height+"\" assetName=\""+mab.mapAssetVO.assetName+"\" isGrid=\""+mab.mapAssetVO.isGrid+"\" isFloor=\""+mab.mapAssetVO.isFloor+"\" isBottom=\""+mab.mapAssetVO.isBottom+"\" isMirror=\""+mab.mapAssetVO.isMirror+"\" />";
			}
			content+="		</asset>";
			content+="	</map>";
			content+="</root>";
			fileStream.writeUTFBytes(content);
			fileStream.close();
			fileStream=null;
			file=null;
			
			_model.isNewMap=false;
			_model.mapFilePath=path;
		}
		
		/**
		 *打开已有的地图文件 
		 */		
		private function openMap():void
		{
			var file:File = File.applicationDirectory;
			var openFileFilter:FileFilter = new FileFilter("XML", "*.xml");
			file.browseForOpen("请选择要打开的地图文件", [openFileFilter]);
			file.addEventListener(Event.SELECT, __onOpenSelectHandle);
		}
		
		/**
		 *打开文件选择完成后 
		 */		
		private function __onOpenSelectHandle(evt:Event):void
		{
			var file:File = evt.target as File;
			_model.mapFilePath=file.nativePath;
			var fileStream:FileStream=new FileStream();
			fileStream.open(file,FileMode.READ);
			var mapXml:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
			fileStream.close();
			fileStream=null;
			file=null;
			
			
			parseMapFile(mapXml);
		}
		
		private function testOpenMap():void
		{
			var str:String="<root>	<name></name>	<description></description>	<background>null</background>	<row>50</row>	<cols>50</cols>	<size>30</size>	<map>		<road>1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0</road>		<asset assetLibPath=\"map/mapAsset.xml\">			<item link=\"asset.map.grass.assetGrass3\" x=\"248\" y=\"0\" z=\"190\" width=\"51\" height=\"37\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.building.assetBuilding1\" x=\"625.5\" y=\"0\" z=\"320.5\" width=\"220\" height=\"227\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetGrass7\" x=\"537.5\" y=\"0\" z=\"440.5\" width=\"57\" height=\"54\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetGrass10\" x=\"798\" y=\"0\" z=\"304\" width=\"50\" height=\"77\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetGrass6\" x=\"680\" y=\"0\" z=\"460\" width=\"50\" height=\"50\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetTree1\" x=\"879\" y=\"0\" z=\"167\" width=\"83\" height=\"107\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetTree8\" x=\"771\" y=\"0\" z=\"431\" width=\"50\" height=\"87\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetTree10\" x=\"408\" y=\"0\" z=\"416\" width=\"52\" height=\"91\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetTree7\" x=\"631.5\" y=\"0\" z=\"600.5\" width=\"50\" height=\"87\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetTree3\" x=\"395.5\" y=\"0\" z=\"308.5\" width=\"69\" height=\"100\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetTree2\" x=\"513\" y=\"0\" z=\"557\" width=\"114\" height=\"108\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetTree5\" x=\"942\" y=\"0\" z=\"346\" width=\"103\" height=\"153\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetTree6\" x=\"716.5\" y=\"0\" z=\"171.5\" width=\"103\" height=\"153\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"null\" x=\"-2\" y=\"0\" z=\"120\" width=\"148\" height=\"70\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />			<item link=\"asset.map.grass.assetGrass1\" x=\"870\" y=\"0\" z=\"434\" width=\"148\" height=\"70\" isGrid=\"false\" isFloor=\"false\" isBottom=\"false\" isMirror=\"false\" />		</asset>	</map></root>";
			
			parseMapFile(XML(str));
		}
		
		/**
		 *解析地图文件 
		 */		
		private function parseMapFile(xml:XML):void
		{
			//取得地图基本信息
			_model.mapVO.mapName=xml.name;
			_model.mapVO.mapType=int(xml.mapType);
			_model.mapVO.mapDescription=xml.description;
			_model.mapVO.mapRow=xml.row;
			_model.mapVO.mapCols=xml.cols;
			_model.mapVO.cellSize=xml.size;
			
			var assetXml:XML=XML(xml.map.asset);
			
			//设置资源库信息
			var arrLib:Array=assetXml.@assetLibPath.toString().split(',');
			_model.mapVO.assetLibPath=[];
			for(var i:int=0;i<arrLib.length;i++)
			{
				_model.mapVO.assetLibPath[i]=new Object();
				_model.mapVO.assetLibPath[i].isLoad=false;//标识当前库是未加载的
				_model.mapVO.assetLibPath[i].path=arrLib[i];//指定库路径
			}
			
			//清除原物件数据
			for(var m:int=0;m<_model.mapVO.assetArray.length;m++)
			{
				if(_model.mapVO.assetArray[m])
				{
					if(_model.mapVO.assetArray[m].parent) _model.mapVO.assetArray[m].parent.removeChild(_model.mapVO.assetArray[m]);
					_model.mapVO.assetArray[m].dispose();
				}
				_model.mapVO.assetArray[m]=null;
			}
			
			//创建物件数据
			_model.mapVO.assetArray=new Vector.<MapAssetBase>();
			var arrAsset:XMLList=assetXml.item;
			if(arrAsset && arrAsset.length()>0)
			{
				for(var x:int=0;x<arrAsset.length();x++)
				{
					var basePoint:Point=null;
					if(arrAsset[x].hasOwnProperty("@baseX") && arrAsset[x].hasOwnProperty("@baseY"))
					{
						basePoint=new Point(Number(arrAsset[x].@baseX), Number(arrAsset[x].@baseY));
					}
					
					var mab:MapAssetBase=new MapAssetBase(arrAsset[x].@link, false, basePoint);
					mab.mapAssetVO.assetName=arrAsset[x].@assetName;
					mab.mapAssetVO.isBottom=arrAsset[x].@isBottom=="true";
					mab.mapAssetVO.isFloor=arrAsset[x].@isFloor=="true";
					mab.mapAssetVO.isGrid=arrAsset[x].@isGrid=="true";
					mab.mapAssetVO.isMirror=arrAsset[x].@isMirror=="true";
					mab.mapAssetVO.link=arrAsset[x].@link;
					mab.position=new IsoPoint(arrAsset[x].@x, arrAsset[x].@y, arrAsset[x].@z);
					//					mab.width=Number(arrAsset[x].@width);
					//					mab.height=Number(arrAsset[x].@height);
					_model.mapVO.assetArray.push(mab);
				}
			}
			
			//帧听所有库资源是否加载完成
			_assetPanelView.addEventListener(MapEditorEvent.UPDATE_BY_ASSET_LIB_PATH_ASSET_LOAD_COMPLETE, __onUpdateByAssetLibPathAssetLoadComplete);
			_assetPanelView.updateByAssetLibPath(_model.mapVO.assetLibPath);
			
			_mapSceneView.updateMapGrid();//更新网格
			_mapSceneView.updateMapRoad(xml.map.road.toString().split(','))//更新路点
		}
		
		private function __onUpdateByAssetLibPathAssetLoadComplete(evt:MapEditorEvent):void
		{
			_assetPanelView.removeEventListener(MapEditorEvent.UPDATE_BY_ASSET_LIB_PATH_ASSET_LOAD_COMPLETE, __onUpdateByAssetLibPathAssetLoadComplete);
			_mapSceneView.updateMapAsset();//更新地图资源物件
		}
	}
}